package com.anrongtec.zcpt.view;

import com.anrongtec.zcpt.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * 
 * @ClassName: LoadingView
 * @author dongtianhao
 * @date 2017年3月29日 上午11:25:22
 *
 */
public class LoadingView extends LinearLayout implements View.OnClickListener {
	public static final int LOADING = 0;
	public static final int STOP_LOADING = 1;
	public static final int NO_DATA = 2;
	public static final int NO_NETWORK = 3;
	public static final int GONE = 4;
	public static final int LOADING_DIALOG = 5;

	private ImageView iv_loading_view_no_net;
	private ProgressBar pb_loading_data;
	private LinearLayout ll_loading_view_error;
	private LinearLayout ll_loading_view_loading_data;
	// private AnimationDrawable mAni;
	private View mView;

	private int status;// 状态值

	private OnRefreshListener mListener;

	/** 消失动画 */
	private Animation out;
	private TextView tvError;

	public LoadingView(Context context) {
		this(context, null);
	}

	public LoadingView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context);
	}

	private void init(Context context) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mView = inflater.inflate(R.layout.view_loading, this);
		iv_loading_view_no_net = (ImageView) mView.findViewById(R.id.iv_loading_view_no_net);
		pb_loading_data = (ProgressBar) mView.findViewById(R.id.pb_loading_data);
		ll_loading_view_loading_data = (LinearLayout) mView.findViewById(R.id.ll_loading_view_loading_data);
		ll_loading_view_error = (LinearLayout) mView.findViewById(R.id.ll_loading_view_error);
		// mAni = (AnimationDrawable) imageView.getBackground();

		tvError = (TextView) mView.findViewById(R.id.tv_loading_view_error);
		// String exchange =
		// getResources().getString(R.string.click_to_refresh);
		// tvError.setText(Html.fromHtml(exchange));
		tvError.setText("点击刷新...");
		mView.setOnClickListener(this);
		mView.setClickable(true);
		setStatue(GONE);
		out = AnimationUtils.loadAnimation(context, android.R.anim.fade_out);
	}

	public void setRefrechListener(OnRefreshListener mListener) {
		this.mListener = mListener;
	}

	public interface OnRefreshListener {
		void refresh();
	}

	public void setStatue(int status) {
		setVisibility(View.VISIBLE);
		this.status = status;
		// Log.d("loadingView", "status: " +status);
		try {
			if (status == LOADING) {// 更新
				ll_loading_view_error.setVisibility(View.GONE);
				ll_loading_view_loading_data.setVisibility(View.VISIBLE);
				// mAni.start();
				mView.setClickable(false);
			} else if (status == STOP_LOADING) {
				// mAni.stop();
				setVisibility(View.GONE);
			} else if (status == NO_DATA) {// 无数据情况
				// mAni.stop();
				ll_loading_view_error.setVisibility(View.VISIBLE);
				iv_loading_view_no_net.setVisibility(View.GONE);
				ll_loading_view_loading_data.setVisibility(View.GONE);
				tvError.setText("暂无数据...");
				mView.setClickable(false);
			} else if (status == NO_NETWORK) {// 无网络情况
				// mAni.stop();
				ll_loading_view_error.setVisibility(View.VISIBLE);
				iv_loading_view_no_net.setVisibility(View.GONE);
				ll_loading_view_loading_data.setVisibility(View.GONE);
				tvError.setText("网络连接异常...");
				mView.setClickable(true);
			} else {
				// mAni.stop();
				setAnimation(out);
				setVisibility(View.GONE);
			}
		} catch (OutOfMemoryError e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onClick(View v) {
//		switch (status) {
//		case NO_NETWORK:
//			if (null != mListener) {
//				mListener.refresh();
//			}
//			setStatue(LOADING);
//			break;
//		}

	}

}
